﻿using Client.ICommandNotify;
using Client.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel
{
    public class Base_AttachmentModel : NotifyBase
    {

		public Base_AttachmentModel()
		{
			QueryCommand = new Command(QueryCommand_, canQueryCommand_);
            Deletecommand = new Command(Deletecommand_, canDeletecommand_);
        }

        private bool canDeletecommand_()
        {
            return true;
        }

        private void Deletecommand_(object obj)
        {
           if(obj != null)
            {
                var data = obj as Attachment;
                if (data == null) return;
                var dbContext = HelpTools.IOC.Getobject<SqlServer.DbContext>();
                dbContext.SqlConfigstr = "Data Source=192.168.100.25;Initial Catalog=SRBG_Basic;User ID=pcwp;Password=pcwpasdw@123";
                int d= dbContext.Update($"DELETE FROM [dbo].[Base_Attachment] WHERE CONVERT(NVARCHAR(MAX), [AttachmentId]) = N'{data?.AttachmentId}'");
                if(d > 0 )
                {
                    QueryCommand_(null);//从新获取数据
                }

            }
        }

        private bool canQueryCommand_()
        {
            return !string.IsNullOrWhiteSpace(Billid);
        }

        private void QueryCommand_(object obj)
        {
          var  dbContext = HelpTools.IOC.Getobject<SqlServer.DbContext>();
            dbContext.SqlConfigstr = "Data Source=192.168.100.25;Initial Catalog=SRBG_Basic;User ID=pcwp;Password=pcwpasdw@123";
            FileList = dbContext.Where<Attachment>($"SELECT *FROM Base_Attachment WHERE CorrelationId = '{Billid}'");
        }

        private List<Attachment> FileList_;

        public List<Attachment> FileList
        {
            get { return FileList_; }
            set { FileList_ = value;
                SetProperty(ref FileList_, FileList);
            }
        }

        private string Billld_;
		/// <summary>
		/// 
		/// </summary>
		public string Billid
        {
			get { return Billld_; }
			set { Billld_ = value;
                SetProperty(ref Billld_, Billid);
            }
		}

        public Command QueryCommand { get; set; }

        public Command Deletecommand { get; set; }
    }
}
